﻿<%@ page language="java"
         import="com.zhuozhengsoft.pageoffice.OpenModeType,com.zhuozhengsoft.pageoffice.PageOfficeCtrl,com.zhuozhengsoft.pageoffice.wordwriter.DataRegion,com.zhuozhengsoft.pageoffice.wordwriter.WordDocument"
         pageEncoding="utf-8" %>
<%

    //设置字符集
    request.setCharacterEncoding("utf-8");

    PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
    //设置服务器页面
    poCtrl.setServerPage(request.getContextPath() + "/poserver.zz");
    WordDocument wordDoc = new WordDocument();

    //打开数据区域，openDataRegion方法的参数代表Word文档中的书签名称
    DataRegion dataRegion1 = wordDoc.openDataRegion("PO_userName");

    //设置DataRegion的可编辑性
    dataRegion1.setEditing(true);
    DataRegion dataRegion2 = wordDoc.openDataRegion("PO_deptName");
    dataRegion2.setEditing(true);
    poCtrl.setWriter(wordDoc);

    //获取文件名 和路径
    String url = request.getParameter("url");
    String fileName = request.getParameter("fileName");
    String type = request.getParameter("type");  // 1 是可以编辑的状态  ，其他是查看状态

    //设置隐藏Office 标题栏 菜单栏 Office工具条
    poCtrl.setTitlebar(false); //隐藏标题栏
    poCtrl.setMenubar(false); //隐藏菜单栏
    poCtrl.setOfficeToolbars(false);//隐藏Office工具条

    //判断是预览还是编辑  如果是编辑 设置是否需要展示菜单栏
    if ("1".equals(type)) {
        //添加自定义按钮
        poCtrl.addCustomToolButton("保存", "Save", 1);
        //设置保存文档的页面
        poCtrl.setSaveFilePage("SaveFile.jsp?url=" + url + "&fileName=" + fileName);
        poCtrl.addCustomToolButton("开始手写", "StartHandDraw()", 5);
    } else {
        poCtrl.setCustomToolbar(false);//隐藏自定义工具栏
    }

    //打开Word文档,当需要同时保存数据和保存文档时,OpenModeType必须是docSubmitForm模式。
    poCtrl.webOpen(request.getSession().getServletContext().getRealPath("file/") + url + "/" + fileName, OpenModeType.docSubmitForm, "张佚名");

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title id="title">在线查看</title>

    <script type="text/javascript">
        let TYPE;

        //文档关闭前先提示用户是否保存
        function BeforeBrowserClosed() {
            if (document.getElementById("PageOfficeCtrl1").IsDirty) {
                if (TYPE === '1' || TYPE === 1) {
                    return confirm("提示：是否退出编辑 ？");
                } else {
                    return confirm("提示：是否退出预览 ？");
                }
            }
        }

        //开始手写
        function StartHandDraw() {
            document.getElementById("PageOfficeCtrl1").HandDraw.Start();
        }

        //保存文件
        function Save() {
            document.getElementById("PageOfficeCtrl1").WebSave();
        }

        //撤销最近一次手写
        function UndoHandDraw() {
            document.getElementById("PageOfficeCtrl1").HandDraw.Undo();
        }

        //退出手写
        function ExitHandDraw() {
            document.getElementById("PageOfficeCtrl1").HandDraw.Exit();
        }
    </script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/jquery.min.js"></script>
    <script src="layui/layui.js" type="javascript"></script>
    <link type="text/css" href="layui/css/layui.css" rel="stylesheet">
    <script type="text/javascript">
        $(function () {
            let type = '${param.type}';
            TYPE = type;
            let fileName = '${param.fileName}'
            if (type === '1') {
                $("#title").html(fileName + "--在线批注")
            } else {
                $("#title").html(fileName + "--在线预览")
            }
        });
    </script>
</head>
<body>
<% if ("1".equals(type)) { %>
<div class="layui-row" style="padding: 5px">
    <button id="Button3" type="button" class="layui-btn layui-btn-sm" onclick="Save()">
        <i class="layui-icon">&#x1005;</i>
        保存
    </button>

    <button id="Button5" type="button" class="layui-btn layui-btn-sm layui-btn-normal" onclick="StartHandDraw()">
        <i class="layui-icon">&#xe6b2;</i>
        批注
    </button>
    <button id="Button1" type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="UndoHandDraw()">
        <i class="layui-icon">&#xe65c;</i>
        撤销
    </button>
    <button id="Button2" type="button" class="layui-btn layui-btn-sm layui-btn-danger" onclick="ExitHandDraw()">
        <i class="layui-icon">&#x1006;</i>
        退出手写
    </button>
</div>
<%}%>

<form id="form1">
    <div style="width: auto; height: 700px;">
        <%=poCtrl.getHtmlCode("PageOfficeCtrl1")%>
    </div>
</form>
</body>
</html>
